
<?php
$saveOrder = ($ordering=='ordering');
?>
<?= formOpen(site_url('admin/post/index'), 'post', 'id="adminForm" class="formAjax"') ?>
	<div class="table-responsive">
		<table class="table table-hover table-striped" id="tableList">
			<thead>
				<tr>
					<th width="1%" class="text-center nowrap"><?= filtersort($link, lang('id'), 'id', $direction, $ordering) ?></th>
					<th width="1%" class="text-center"><?= filtersort($link, lang('status'), 'published', $direction, $ordering) ?></th>
					<th><?= filtersort($link, lang('title'), 'title', $direction, $ordering) ?></th>
					<th><?= filtersort($link, lang('category'), 'category_id', $direction, $ordering) ?></th>
					<th><?= filtersort($link, lang('created_by'), 'created_by', $direction, $ordering) ?></th>
					<th><?= filtersort($link, lang('created_time'), 'created_time', $direction, $ordering) ?></th>
					<th width="1%" class="text-center"><?= filtersort($link, lang('hit'), 'hit', $direction, $ordering) ?></th>
					<th width="1%" class="text-center"><?= filtersort($link, lang('type'), 'type', $direction, $ordering) ?></th>
					<th width="1%" class="text-center"><?= filtersort($link, lang('approve'), 'approved', $direction, $ordering) ?></th>
					<th width="1%" class="text-center nowrap"><span class="sr-only"><?= lang('delete') ?></span></th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td colspan="20" class="text-center">
						<nav>
							<?php echo $this->pagination->create_links(); ?>
						</nav>
					</td>
				</tr>
			</tfoot>
			<tbody class="sortable">
			<?php
			$order = array();
			foreach($items as $k=>$item):
				$order[] = $item->ordering;
				$item_link = siteUrl('post/detail/'.$item->id.'-'.$item->alias, $item->category_id);
			?>
				<tr data-id="<?= $item->id ?>">
					<td width="1%" class="text-center"><?= $item->id ?></td>
					<td width="1%" class="text-center">
						<a href="<?= site_url('admin/post/toggle/published/'.$item->id.'/'.(1-$item->published)) ?>" class="btn btn-default btn-micro hasTip toggle linkAjax" title="<?= lang('toggle_to_publish') ?>"><i class="icon-<?php if(!$item->published) echo 'un' ?>publish"></i></a>
					</td>
					<td>
						<?= anchor('admin/post/form/'.$item->id, $item->title) ?>
						<span class="pull-right"> &nbsp;<a target="_blank" class="hasTip" href="<?= $item_link ?>" title="<?= lang('view_front') ?>"><i class="icon-out-2"></i></a></span><?php if($item->approved): ?> <span class="pull-right"> &nbsp;<i class="text-success icon-checkmark hasTip" title="<?= lang('approved') ?>"></i></span><?php endif ?><?php if(strtotime($item->published_up) > time()): ?> <span class="pull-right"><i class="text-warning icon-clock hasTip" title="<?= lang('published_up') ?>: <?= mdate(lang('date_format_lc'), strtotime($item->published_up)) ?>"></i></span><?php endif ?>
					</td>
					<td><?= $item->category->title ?></td>
					<td><?= $item->creator->fullname ?></td>
					<td><?= mdate(lang('date_format_lc'), strtotime($item->created_time)) ?></td>
					<td width="1%" class="text-center"><?= $item->hit ?></td>
					<td width="1%" class="text-center nowrap"><?= $type[$item->type] ?></td>
					<td width="1%" class="text-center">
						<a href="<?= site_url('admin/post/toggle/approved/'.$item->id.'/'.(1-$item->approved)) ?>" class="btn btn-default btn-micro hasTip toggle linkAjax" title="<?= lang('toggle_to_approve') ?>"><i class="icon-<?php if(!$item->approved) echo 'un' ?>publish"></i></a>
					</td>
					<td width="1%" class="text-center">
						<a href="<?= site_url('admin/post/delete/'.$item->id) ?>" class="btn btn-default btn-micro hasTip linkAjax" title="<?= lang('delete_this') ?>" data-confirm="<?= lang('sure_delete_this') ?>"><i class="icon-trash"></i></a>
					</td>
				</tr>
			<?php endforeach ?>
			</tbody>
		</table>
	</div>
	<div id="data-ordering" data-ordering="<?= implode(',',$order); ?>"></div>
<?= formClose() ?>

<script type="text/javascript">

/* when the DOM is ready */
$(document).ready(function(){
	$('tbody.sortable').sortable({
		handle: '>.sortable-handler',
		opacity: 0.7,
		update: function(){
			var data_order = $('#data-ordering').data('ordering');
			var data_id = $(this).sortable('toArray',{ attribute: 'data-id'});
			console.log(data_order);
			console.log(data_id.join(','));
		}
	});
});
</script>
